package com.wandian.setting.mvvm.usercenter;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.adapter.BasePagerAdapter;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.dialog.BottomRecyclePopupWindow;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.DialogUtil;
import com.mylib.libcore.utils.GlideUtil;
import com.mylib.libcore.utils.ScreenUtils;
import com.mylib.libcore.utils.UIUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wandian.setting.R;
import com.wandian.setting.adapter.UserInfoCZRecycleAdapter;
import com.wandian.setting.adapter.UserInfoDTRecycleAdapter;
import com.wandian.setting.adapter.UserInfoSDRecycleAdapter;
import com.wandian.setting.adapter.UserInfoWLRecycleAdapter;
import com.wandian.setting.databinding.SetActivityUserHeadinfoBinding;
import com.wandian.setting.mvvm.usercenter.czpagefragment.CZPageFragment;
import com.wandian.setting.mvvm.usercenter.dtpagefragment.DTPageFragment;
import com.wandian.setting.mvvm.usercenter.sdpagefragment.SDPageFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description  头部信息
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_SETTING_USERHEADINFO)
public class UserHeadInfoActivity extends BaseMVVMActivity<UserHeadInfoViewModel>  implements BottomRecyclePopupWindow.ItemClink {
    private SetActivityUserHeadinfoBinding dataBinding;
    private BasePagerAdapter myAdapter;
    private List<String> titleList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private ArrayList<String> listType = new ArrayList<>();
    private String title = "动态";
    private String user_id = "";
    private String user_name = "";
    private String user_head = "";
    private String user_des = "";
    private String form_id = "";
    private boolean isFirst = true;
    private int sys_status_height = 0;
    private String isMine = "1";
    private int selectPage = 0;
    private UserInfoBean userInfoBean;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.set_activity_user_headinfo);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        userInfoBean = AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
        try {
            Bundle bundle = getIntent().getExtras();
            if(bundle != null){
                title = bundle.getString("title");
                user_id = bundle.getString("user_id");
                user_name = bundle.getString("user_name");
                user_head = bundle.getString("user_head");
                form_id = bundle.getString("form_id");
                user_des = bundle.getString("user_des");
                if(!UIUtils.isEmpty(user_id) && !UIUtils.isEmpty(form_id) && user_id.equals(form_id)){
                    isMine = "1";
                }else {
                    isMine = "0";
                }
            }
        }catch (Exception e){}
        if(UIUtils.isEmpty(title))title = "动态";
        listType.add("动态");
        listType.add("树洞");
        initViewData();
        initTabViewPage();
        initClink();
    }

    private void initTabViewPage() {
        fragments.clear();
        titleList.clear();
        titleList.add("动态");
        fragments.add(DTPageFragment.newInstance("动态",user_id,user_name,user_head,form_id));
        if(isMine.equals("1")) {
            titleList.add("树洞");
            fragments.add(SDPageFragment.newInstance("树洞",user_id,user_name,user_head,form_id));
        }
        titleList.add("成长");
        fragments.add(CZPageFragment.newInstance("成长",user_id,user_name,user_head,form_id));
//        titleList.add("围栏");
        myAdapter = new BasePagerAdapter(getSupportFragmentManager(), fragments,titleList);
        dataBinding.viewPager.setAdapter(myAdapter);
        dataBinding.viewPager.setOffscreenPageLimit(titleList.size());
        dataBinding.tabLayoutTitle.setViewPager(dataBinding.viewPager);
        if(!UIUtils.isEmpty(myAdapter))myAdapter.notifyDataSetChanged();
        dataBinding.viewPager.setOffscreenPageLimit(titleList.size()); //预加载数量
        dataBinding.tabLayoutTitle.setViewPager(dataBinding.viewPager);
       if(selectPage<titleList.size()) dataBinding.viewPager.setCurrentItem(selectPage);

        dataBinding.tabLayoutTitle.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectPage = position;
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectPage = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(isFirst){
            isFirst = false;
            return;
        }else {
            initViewData();
            initTabViewPage();
        }
    }

    private void initViewData(){
        sys_status_height =  ScreenUtils.getStatusHeight(this);
        if(sys_status_height<=0)sys_status_height = 20;
        ViewGroup.LayoutParams linearParams =(ViewGroup.LayoutParams) dataBinding.toolbar.getLayoutParams(); //取控件textView当前的布局参数 linearParams.height = 20;// 控件的高强制设成20
        linearParams.height = sys_status_height+UIUtils.dp2px(45);
        dataBinding.toolbar.setLayoutParams(linearParams);
        dataBinding.toolbar.setPadding(0,UIUtils.px2dp(sys_status_height),0,0);
        dataBinding.bindTool.mRelativeLayoutTitle.setPadding(0,UIUtils.px2dp(sys_status_height),0,0);
        if(!UIUtils.isEmpty(user_id) && !UIUtils.isEmpty(form_id) && !user_id.equals(form_id)){
            dataBinding.bindTool.toolBarTvTitle.setText(user_name+"");
            GlideUtil.into(this,user_head,dataBinding.niceUserHead);
            dataBinding.tvUserDes.setText(user_des+"");
            dataBinding.tvUserName.setText(user_name+"");
            dataBinding.tvSendDt.setVisibility(View.GONE);
        }else {
            dataBinding.bindTool.toolBarTvTitle.setText("个人中心");
            GlideUtil.into(this,userInfoBean.getHeadPic(),dataBinding.niceUserHead);
            dataBinding.tvUserDes.setText(userInfoBean.getDescription()+"");
            dataBinding.tvUserName.setText(userInfoBean.getNickName()+"");
            dataBinding.tvSendDt.setVisibility(View.VISIBLE);
        }
    }


    private void initClink(){
        dataBinding.bindTool.toolBarIvBack.setOnClickListener(v -> finish());
        dataBinding.ivBack.setOnClickListener(v -> finish());

        dataBinding.appbarChannel.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
                int sY = Math.abs(i);
                if(sY<dataBinding.ivTopBg.getHeight()){
                    dataBinding.toolbar.setAlpha((float)sY/dataBinding.ivTopBg.getHeight());
                }else dataBinding.toolbar.setAlpha(1f);
            }
        });
        dataBinding.tvSendDt.setOnClickListener(v ->  DialogUtil.showCommonBottomRecycleDialog(dataBinding.coordinatorLayout,this::itemTextClink,"HeadInfo",listType));
   }


    @Override
    public void itemTextClink(int index, String text, String id, String type) {
        if(!UIUtils.isEmpty(text)){
            ARouter.getInstance().build(RouterConfig.ROUTER_ACTIVITY_SETTING_SEND_DYNAMIC).withString("title",text).navigation();
        }
    }
}
